extends Node

# 根据point位置获取id值
func getPointId(x:float,y:float):
	return "("+String.num(x)+","+String.num(y)+")"

# 根据玩家顺序获取相应阵营颜色
func getColorByInt(index:int):
	match index:
		0:
			return Color.ORANGE_RED
		1:
			return Color.MEDIUM_ORCHID
		2:
			return Color.ORANGE

# 获取是否可以控制镜头
func getIsCanControlCamera():
	return ObjectManager.gameManager.operationManager.is_can_control_camera
	
# 获取是否可以点击point
func getIsCanClickPoint():
	return ObjectManager.gameManager.operationManager.is_can_click_point

# 获取我有的全部视野
func getArrPointsHasView():
	var arr_points = Array()
	var gameManager = ObjectManager.gameManager as GameManager
	var map_points = gameManager.map_points
	var player = gameManager.me_player as Player
	# 天眼传真
	if player.is_looking:
		return map_points.values()
	# 小子周围一圈
	if player.alive:
		var smallPoint = player.small.point
		var smallAround = getArrPointsAround(smallPoint)
		# 这里遍历一下，迷雾森林
		var arr_no迷雾 = Array()
		for p in smallAround:
			if !p.hasOthersMark("迷雾森林"):
				arr_no迷雾.append(p)
		arr_points.append_array(arr_no迷雾)
	# 每个大子和大子的周围一圈
	for big in player.map_bigs.values():
		if big == null:
			continue
		var point = big.point
		var bigAround = Array()
		if big.view_area == 2:
			bigAround = getArrPointsAround1And2(point)
		elif big.view_area == 1:
			bigAround = getArrPointsAround(point)
		# 这里遍历一下，迷雾森林
		var arr_no迷雾 = Array()
		for p in bigAround:
			if !p.hasOthersMark("迷雾森林"):
				arr_no迷雾.append(p)
		arr_points.append_array(arr_no迷雾)
		arr_points.append(point)
	# 三个小子
	for p in gameManager.map_players.values():
		if p.alive:
			arr_points.append(p.small.point)
	# 水晶
	for point in map_points.values():
		if point.real_terrain == Terrain.jing:
			arr_points.append(point)
	return arr_points


# 获取当前point周围一圈的point
func getArrPointsAround(point:Point):
	var map_points = ObjectManager.gameManager.map_points
	var arr_points = Array()
	# 6个
	var arr_pointIds = Array()
	arr_pointIds.append((getPointId(point.x-0.5,point.y+1)))
	arr_pointIds.append((getPointId(point.x+0.5,point.y+1)))
	arr_pointIds.append((getPointId(point.x-1,point.y)))
	arr_pointIds.append((getPointId(point.x+1,point.y)))
	arr_pointIds.append((getPointId(point.x-0.5,point.y-1)))
	arr_pointIds.append((getPointId(point.x+0.5,point.y-1)))
	for pointId in arr_pointIds:
		var p = map_points.get(pointId)
		if p != null:
			arr_points.append(p)
	return arr_points

#获取第二圈的point
func getArrPointsAround2(point:Point):
	var map_points = ObjectManager.gameManager.map_points
	var arr_points = Array()
	# 12个
	var arr_pointIds = Array()
	arr_pointIds.append((getPointId(point.x-1,point.y+2)))
	arr_pointIds.append((getPointId(point.x,point.y+2)))
	arr_pointIds.append((getPointId(point.x+1,point.y+2)))
	arr_pointIds.append((getPointId(point.x-1.5,point.y+1)))
	arr_pointIds.append((getPointId(point.x+1.5,point.y+1)))
	arr_pointIds.append((getPointId(point.x-2,point.y)))
	arr_pointIds.append((getPointId(point.x+2,point.y)))
	arr_pointIds.append((getPointId(point.x-1.5,point.y-1)))
	arr_pointIds.append((getPointId(point.x+1.5,point.y-1)))
	arr_pointIds.append((getPointId(point.x-1,point.y-2)))
	arr_pointIds.append((getPointId(point.x,point.y-2)))
	arr_pointIds.append((getPointId(point.x+1,point.y-2)))
	for pointId in arr_pointIds:
		var p = map_points.get(pointId)
		if p != null:
			arr_points.append(p)
	return arr_points

# 获取第一圈+第二圈的point
func getArrPointsAround1And2(point:Point):
	var arr_total = Array()
	var arr_points_1 = getArrPointsAround(point)
	var arr_points_2 = getArrPointsAround2(point)
	arr_total.append_array(arr_points_1)
	arr_total.append_array(arr_points_2)
	return arr_total

# 全部沙漠地形
func getArrPointsSha():
	var map_points = ObjectManager.gameManager.map_points
	var arr_points = Array()
	for p in map_points.values():
		if p.real_terrain == 4 && p.is_view:
			arr_points.append(p)
	return arr_points

# 待点击动画整齐
func neatAnimation():
	var operationManager = ObjectManager.gameManager.operationManager
	for p in operationManager.arr_listeningPoints:
			p.waitClick()

# 镜头是否已经拉到最大
func getCameraIsZoomMax():
	return ObjectManager.gameManager.camera.is_zoom_max

# 能量判断
func checkActionEnough(needEnergy:int):
	var gameManager = ObjectManager.gameManager as GameManager
	var player = gameManager.me_player as Player
	var small = player.small as Small
	if small.energy < needEnergy:
		ObjectManager.toast.showToast("能量不足，需要"+String.num_int64(needEnergy)+"点")
		ObjectManager.gameManager.ui.playSwordOrderChangeAnim()
		return false
	return true

# 可行动判断
func checkIsReadyAction():
	var gameManager = ObjectManager.gameManager as GameManager
	var operationManager = gameManager.operationManager as OperationManager
	var point = operationManager.selectedPoint
	if point.small != null:
		if point.small.is_ready:
			return true
		else:
			ObjectManager.toast.showToast("棋子还没准备好")
			return false
	elif point.big != null:
		if point.big.actionTime > 0:
			return true
		else:
			ObjectManager.toast.showToast("棋子还没准备好")
			return false
	return false

# 幽灵二转判断
func checkIs幽灵CanEvolve():
	var gameManager = ObjectManager.gameManager as GameManager
	var player = gameManager.me_player as Player
	var arr_bigs = player.map_bigs as Dictionary
	var num = 0
	for big in arr_bigs.values():
		if big.evolution == "幽灵":
			num += 1
	if num >= 3:
		return true
	else :
		ObjectManager.toast.showToast("幽灵数量不足3个")
		return false

# 炮台二转判断
func checkIs炮台CanEvolve():
	var gameManager = ObjectManager.gameManager as GameManager
	var player = gameManager.me_player as Player
	var arr_bigs = player.map_bigs as Dictionary
	var num = 0
	for big in arr_bigs.values():
		if big.evolution == "炮台":
			num += 1
	if num >= 3:
		return true
	else :
		ObjectManager.toast.showToast("炮台数量不足3个")
		return false

# 沙兵二转判断
func checkIs沙兵CanEvolve():
	var gameManager = ObjectManager.gameManager as GameManager
	var player = gameManager.me_player as Player
	var arr_bigs = player.map_bigs as Dictionary
	var num = 0
	for big in arr_bigs.values():
		if big.evolution == "沙兵":
			num += 1
	if num >= 3:
		return true
	else :
		ObjectManager.toast.showToast("沙兵数量不足3个")
		return false

# 判断能否二转
func checkIsCanSecondEvolve(evolution: String):
	if evolution == "恶灵":
		return checkIs幽灵CanEvolve()
	elif evolution == "狙炮":
		return checkIs炮台CanEvolve()
	elif evolution == "沙皇":
		return checkIs沙兵CanEvolve()
	else :
		print("这个啥转型体，没有写这个")
	return false

# 是否是唯一转型体
func isOnlyEvolution(evolution: String): 
	var list = ["恶灵","狙炮","沙皇","拳师","灵阵","天谕","妖术","鼓舞"]
	if list.has(evolution):
		return true
	return false

# 是否已经有这个转型体
func checkHasThisEvolution(evolution: String):
	var gameManager = ObjectManager.gameManager as GameManager
	var player = gameManager.me_player as Player
	var arr_bigs = player.map_bigs as Dictionary
	for big in arr_bigs.values():
		if big.evolution == evolution:
			return true
	return false

# 是否有转型数量限制，并且到达上限
func checkEvolveLimitReached(evolution: String):
	var gameManager = ObjectManager.gameManager as GameManager
	var player = gameManager.me_player as Player
	var arr_bigs = player.map_bigs as Dictionary
	var num = 0
	for big in arr_bigs.values():
		if big.evolution == evolution:
			num += 1
	match evolution:
		"顽石":
			return num == 5
	return false

# 获取某个转型体json对象
func getEvolutionJson(title: String):
	var arr_evolution = load("res://游戏/evolution.json").data
	for json in arr_evolution:
		if json.title == title:
			return json

# 判断这个大子有没有儿子
func checkBigHasSon(big: Big):
	var gameManager = ObjectManager.gameManager as GameManager
	var player = gameManager.me_player as Player
	var arr_bigs = player.map_bigs as Dictionary
	for b in arr_bigs.values():
		var bb = b as Big
		if bb.father_big == big:
			return true
	return false
